<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 15-4-21
 * Time: 下午10:41
 */

class article extends adminBase{
    function __construct() {
        parent::__construct();
        $this->cms->setPageTitle("新闻管理");
        $this->cms->setControlFile("admin/tpl/news/news_control.json");
        $admin_info=$this->user->getLoginInfo();
        $user=UserMode::init();
        $levels=$user->getLevels($admin_info['id']);
        if(!$user->checkLevels(14,$levels)){
            $r['message']="您不具有此权限！";
            View::displayAsHtml($r,"plugin/state/tips.php");
            exit();
        }
    }
    function indexTask($page, $type='system'){
        $article=ArticleModule::init('article');
        $page=$page>=1? $page: 1;
        $countOfPage=20;
        $offset=($page-1)*$countOfPage;
        list($r['list'],$total)=$article->getList($type,$countOfPage,$offset);
        $r['type']=$type;
        $r['page']=new Page($page,$total,$countOfPage);
        $url=WebRouter::init()->getQuestion("type={$type}&page=");
        $r['page']->setPageCallback(function($page)use($url){
            return $url.$page;
        });
        $this->cms->tableScene($r,"admin/tpl/news/list.php");
    }

    function addTask(){
        $r=array();
        $this->cms->normalScene($r,"admin/tpl/news/add.php",
            CmsView::TYPE_FORM| CmsView::TYPE_JQUERY| CmsView::TYPE_EDITOR);
    }
    function modifyTask($id){
        $article=ArticleModule::init('article');
        $r=$article->getDetail($id);
        if(empty($r)) return new Intent(404,"文章未找到");
        $this->cms->normalScene($r,"admin/tpl/news/modify.php",
            CmsView::TYPE_FORM| CmsView::TYPE_JQUERY| CmsView::TYPE_EDITOR);
    }
    function submitTask($id,$title,$content){
        $article=ArticleModule::init('article');
        $articleData=$article->getDetail($id);
        if(!empty($articleData)){
            $article->modify($id, $title, $content);
            $r['message']="添加/修改成功";
        }else{
            $article->add($title,$content,'news');
            $r['message']="添加/修改成功";
        }
        $r['url']=WebRouter::init()->getAction('index');
        View::displayAsHtml($r,"plugin/state/tips.php");
    }

    function deleteTask($id){
        $article=ArticleModule::init('article');
        $re=$article->delete($id);
        if($re){
            $r['message']="删除成功";
        }else{
            $r['message']="删除失败，请重试";
        }
        View::displayAsHtml($r,"plugin/state/tips.php");
    }
} 